package org.czl.leetcode2;

import java.util.HashMap;

public class l1297 {
    long[] pow = new long[100003];
    long[] hash = new long[100003];
    int base = 499;
    int ans = 0;
    public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {
        int n = s.length();
        char[] s1 = s.toCharArray();
        hash[0] = s1[0] - 'a' + 1;
        pow[0] = 1;
        for(int i = 1; i < n; i++){
            hash[i] = hash[i - 1] * base + s1[i] - 'a' + 1;
            pow[i] = pow[i - 1] * base;
        }
        for(int k = minSize; k<=maxSize; k++){
            f(k, s1, maxLetters);
        }
        return ans;

    }
    void f(int k, char[] s1, int t){
        int[] set = new int[26];
        int n = s1.length;
        int type = 0;
        HashMap<Long, Integer> map = new HashMap<>();
        for(int i = 0; i < n; i++){
            char ret = s1[i];
            set[ret - 'a']++;
            if(set[ret - 'a'] == 1){
                type++;
            }
            if(i < k - 1){
                continue;
            }
            if(type <= t){
                long co = hash[i]; // l 为i - k + 1 左边那个就位i - k
                if(i - k >= 0){
                    co -= hash[i - k] * pow[k];
                }
                if(!map.containsKey(co)){
                    map.put(co, 0);
                }
                int m = map.get(co) + 1;
                ans = Math.max(ans, m);
                map.put(co, m);
            }
            //出窗口
            set[s1[i - k + 1] - 'a']--;
            if(set[s1[i - k + 1] - 'a'] == 0){
                type--;
            }
        }
    }
}
